package com.fc.example;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialListener;

public class InterstitialActivity extends AppCompatActivity {

    public String mMediaId; // 融合广告位
    ATInterstitial mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);

        this.mMediaId = "b659bbd4265cc3";

        ((TextView) findViewById(R.id.tv_media_id)).setText(getResources().getString(R.string.ad_mediation_id, mMediaId));

        // 加载广告
        this.findViewById(R.id.bt_load).setOnClickListener(it -> loadInterstitialFullAd());

        // 展示广告
        this.findViewById(R.id.bt_show).setOnClickListener(it -> showInterstitialFullAd());
    }

    private void loadInterstitialFullAd() {
        Toast.makeText(InterstitialActivity.this,"开始加载",Toast.LENGTH_SHORT).show();
        mInterstitialAd = new ATInterstitial(this, mMediaId);
        mInterstitialAd.setAdListener(new ATInterstitialListener() {
            @Override
            public void onInterstitialAdLoaded() {
                Log.d("test", "onInterstitialAdLoaded:");
                Toast.makeText(InterstitialActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onInterstitialAdLoadFail(AdError adError) {
                //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
                //AdError，请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.e("test", "onInterstitialAdLoadFail:" + adError.getFullErrorInfo());
                Toast.makeText(InterstitialActivity.this,"加载失败:" + adError.toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onInterstitialAdClicked(ATAdInfo atAdInfo) {
            }

            @Override
            public void onInterstitialAdShow(ATAdInfo atAdInfo) {
                //ATAdInfo可区分广告平台以及获取广告平台的广告位ID等
                //请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_sdk_callback_access?id=callback_info
                //建议在此回调中调用load进行广告的加载，方便下一次广告的展示（不需要调用isAdReady()）
            }

            @Override
            public void onInterstitialAdClose(ATAdInfo atAdInfo) {
            }

            @Override
            public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {
            }

            @Override
            public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {
            }

            @Override
            public void onInterstitialAdVideoError(AdError adError) {
                //AdError，请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.e("test", "onInterstitialAdVideoError:" + adError.getFullErrorInfo());
            }
        });
        mInterstitialAd.load();
    }

    // 在加载成功后展示广告
    private void showInterstitialFullAd() {
        if (this.mInterstitialAd == null) {
            Log.d("TEST", "请先加载广告或等待广告加载完毕后再调用show方法");
            return;
        }
        /** 5、设置展示监听器，展示广告 */
        this.mInterstitialAd.show(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}